<?php
declare (strict_types = 1);

namespace app\controller;

use app\model\AdminModel;
use app\model\PeeModel;
use think\Request;

class Pee extends Base
{
    //获取所有考研信息数据
    public function getAllPeeInfo(Request $request)
    {

//        parent::initialize();

        $pee_category=$request->param('pee_category');
        $db=new AdminModel();

        if($pee_category==null){

            $info = $db->table('pee_info a,admin b')
                ->where('a.admin_id = b.admin_id')
                ->order('pee_release_time','desc')
                ->select();

            $total=$db->table('pee_info a,admin b')
                ->where('a.admin_id = b.admin_id')
                ->order('pee_release_time','desc')
                ->count('a.pee_id');
        }
        else{

            $page= $request->param('page');
            $limit= $request->param('limit');

            $info = $db->table('pee_info a,admin b')
                ->where('a.admin_id = b.admin_id')
                ->where('a.pee_category',$pee_category)
                ->order('pee_release_time','desc')
                ->page($page)->limit($limit)
                ->select();


            $total= $db->table('pee_info a,admin b')
                ->where('a.admin_id = b.admin_id')
                ->where('a.pee_category',$pee_category)
                ->order('pee_release_time','desc')
                ->page($page)->limit($limit)
                ->count('a.pee_id');

        }

        return json(['code'=>1,'data'=>$info,'total'=>$total]);

    }


//    根据pee_id获取考研信息
    public function getPeeInfoByPeeID(Request $request){

        $pee_id=$request->param('pee_id');
        $db=new PeeModel();
        $pee = $db->table('pee_info a,admin b')
            ->where('a.admin_id = b.admin_id')
            ->where('a.pee_id',$pee_id)
            ->find();

        return json($pee);

    }

//    根据pee_id和pee_ctr增加阅读量
    public function addPeeCtrByPeeID(Request $request){
        $pee_id=$request->param('pee_id');
        $pee_ctr=$request->param('pee_ctr');
        $pee=PeeModel::find($pee_id);

        $data=['pee_ctr'=>$pee_ctr];
        if($pee->save($data)){
            return json(['code'=>1]);
        }

        return json(['code'=>0]);
    }

    //    考研资讯主页获取各个类别的考研资讯的最新五条信息
    public function getNewPeeInfo(Request $request)
    {
//        考研信息
        $page= $request->param('page');
        $limit= $request->param('limit');
        $db=new PeeModel();

        $all_info = $db->table('pee_info a,admin b')
            ->where('a.admin_id = b.admin_id')
            ->order('pee_release_time','desc')
            ->page($page)->limit($limit)->select();

        $kaoyan_info = $db->table('pee_info a,admin b')
            ->where('a.admin_id = b.admin_id')
            ->where('pee_category',1)
            ->order('pee_release_time','desc')
            ->page($page)->limit($limit)->select();

        $recommend_info = $db->table('pee_info a,admin b')
            ->where('a.admin_id = b.admin_id')
            ->where('pee_category',4)
            ->order('pee_release_time','desc')
            ->page($page)->limit($limit)->select();

        $zhaoshen_info = $db->table('pee_info a,admin b')
            ->where('a.admin_id = b.admin_id')
            ->where('pee_category',2)
            ->order('pee_release_time','desc')
            ->page($page)->limit($limit)->select();

        $daily_info = $db->table('pee_info a,admin b')
            ->where('a.admin_id = b.admin_id')
            ->where('pee_category',3)
            ->order('pee_release_time','desc')
            ->page($page)->limit($limit)->select();

        $high_info = $db->table('pee_info a,admin b')
            ->where('a.admin_id = b.admin_id')
            ->order('pee_ctr','desc')
            ->page($page)->limit($limit)->select();


        return json(['code'=>1,'all_info'=>$all_info,'kaoyan_info'=>$kaoyan_info,'recommend_info'=>$recommend_info,
            'zhaoshen_info'=>$zhaoshen_info,'daily_info'=>$daily_info,'high_info'=>$high_info]);
    }

//    根据pee_id修改考研资讯信息
    public function updatePeeInfo(Request $request){

        $admin_id=AdminModel::getAdmindByToken($request->param(('token'))); //获取管理员id

        $EditForm=$request->param(('EditForm'));
        $pee_id=$request->param(('pee_id'));

        $pee= PeeModel::find($pee_id);

        $EditForm['admin_id']=$admin_id;

        if($pee->save($EditForm)){

            return json(['code'=>1]);

        }

        return json(['code'=>0]);

    }

//    根据pee_id删除考研资讯
    public function deletePeeInfo(Request $request){

        $pee_ids=$request->param(('pee_id'));
        $pee= new PeeModel();

        //判断id是数组还是一个数值
        if(is_array($pee_ids)){
            $condition = 'pee_id in('.implode(',',$pee_ids).')';
        }else{
            $condition = 'pee_id='.$pee_ids;
        }

        if($pee->where($condition)->delete()){
            return json(['code'=>1]);
        }

        return json(['code'=>0]);

    }

//   添加考研资讯
    public function addPeeInfo(Request $request){

        $admin_id=AdminModel::getAdmindByToken($request->param(('token'))); //获取管理员id

        $AddForm=$request->param(('AddForm'));

        $pee=new PeeModel();


        $AddForm['admin_id']=$admin_id;


        if($pee->save($AddForm)){

            return json(['code'=>1]);

        }
        return json(['code'=>0]);



    }





    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
